<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
 * This is the model class for table "saleorder".
 *
 * @property integer $orderid
 * @property integer $user_id
 * @property integer $product_id
 * @property integer $auction_id
 * @property double $market_val
 * @property double $price
 * @property double $bid_price
 * @property integer $num
 * @property string $pname
 * @property string $pimg
 * @property string $pspec
 * @property string $name
 * @property string $phone
 * @property string $qq
 * @property string $alipay
 * @property string $aliname
 * @property string $province
 * @property string $city
 * @property string $county
 * @property string $detail
 * @property integer $pay_type
 * @property string $out_trade_no
 * @property string $trade_no
 * @property string $buyer_email
 * @property integer $state
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $remarks
 * @property integer $take_money
 * @property integer $give_money
 * @property User $user
 */

class Saleorder extends ActiveRecord
{
    const STEAT_PATTING = 0;
    const STEAT_PATTED = 1;
    const STEAT_NOT_PATTED = 2;
    const STEAT_PAID = 3;
    const STEAT_SHIPPED = 4;
    const STEAT_RECEIVED = 5;
    const STEAT_POST = 6;

    const PAY_ALIPAY = 1;
    const PAY_WX = 2;
    const PAY_NOTKNOW = 0;

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'state'=>[
                self::STEAT_PATTING=>'竞拍中',
                self::STEAT_PATTED=>'拍中',
                self::STEAT_NOT_PATTED=>'未拍中',
                self::STEAT_PAID=>'已付款',
                self::STEAT_SHIPPED=>'已发货',
                self::STEAT_RECEIVED=>'已收货',
                self::STEAT_POST=>'已晒单',
            ],
            'pay_type'=>[
                self::PAY_ALIPAY=>'支付宝',
                self::PAY_WX =>'微信',
                self::PAY_NOTKNOW =>'未知',
            ]
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'saleorder';
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'orderid' => 'ID',
            'user_id' => '用户',
            'product_id' => '产品',
            'auction_id' => '竞拍',
            'market_val' => '市场价',
            'price' => '成交价',
            'bid_price' => '竞拍价',
            'num' => '竞拍次数',
            'pname' => '产品名称',
            'pimg' => '产品图片',
            'pspec'=>'产品规格',
            'state' => '状态',
            'name' => '收货人姓名',
            'phone' => '电话',
            'qq' => 'qq',
            'alipay' => '支付宝账号',
            'aliname' => '支付宝姓名',
            'province' => '省',
            'city' => '市',
            'county' => '区',
            'detail' => '详情',
            'pay_type' => '支付方式',
            'out_trade_no'=>'流水号',
            'trade_no' => '交易号',
            'buyer_email' => '用户联系方式',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    public static function getOrCreate(Auction $auction,User $user,$toCreate = true)
    {
        Saleorder::updateAll(['price'=>$auction->price],['auction_id'=>$auction->auctionid]);
        $oldModel = self::find()->where(['auction_id'=>$auction->auctionid,'user_id'=>$user->userid])->one();
        if($oldModel)
            return $oldModel;

        if($toCreate === false)
            return false;
        $model = new self();
        $model->user_id = $user->userid;
        $model->product_id = $auction->product_id;
        $model->auction_id = $auction->auctionid;
        $model->market_val = $auction->market_val;
        $model->num = 0;
        $model->pname = $auction->product->name;
        $model->pspec = $auction->product->spec;
        $model->give_money = 0;
        $model->take_money = 0;
        $pic = Pic::find()->where(['product_id'=>$auction->product_id])->orderBy(['picid'=>SORT_ASC])->one();
        $model->pimg = $pic?$pic->url:'';
        $model->state = self::STEAT_PATTING;
        $model->price = $auction->price;
        return $model;
    }

    public function getAuction()
    {
        return $this->hasOne(Auction::className(),['auctionid'=>'auction_id']);
    }

    public function getUser()
    {
        return $this->hasOne(User::className(),['userid'=>'user_id']);
    }

    public function getProduct()
    {
        return $this->hasOne(Product::className(),['pid'=>'product_id']);
    }

    public function canShow()
    {
        if($this->state == self::STEAT_POST)//已晒单
            return false;
        $user = $this->user;
        if(!$user)
            return false;
        if($user->type == User::IS_ROBOT&& $this->state == self::STEAT_PATTED)//机器人可直接晒单
            return true;
        if($user->type == User::IS_USER && $this->state == self::STEAT_RECEIVED)//用户在收货后可晒单
            return true;
        return false;
    }

    /**
     * 状态可手动修改
     */
    public function canChange($state)
    {
        if($this->state==self::STEAT_NOT_PATTED||$this->state==self::STEAT_PATTING||$this->state==self::STEAT_POST)
            return false;
        if($this->state==self::STEAT_PATTED&&$state == self::STEAT_PAID)
            return true;
        if($this->state==self::STEAT_PAID && $state == self::STEAT_SHIPPED)
            return true;
        if($this->state==self::STEAT_SHIPPED && $state == self::STEAT_RECEIVED)
            return true;
        if($this->state==self::STEAT_RECEIVED&& $state==self::STEAT_POST)
            return true;
        return false;
    }

    public function build_order_no(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    public function setAddr(Addr $addr)
    {
        $this->name = $addr->name;
        $this->phone = $addr->phone;
        $this->qq = $addr->qq;
        $this->alipay = $addr->alipay;
        $this->aliname = $addr->aliname;
        $this->province = $addr->province;
        $this->city = $addr->city;
        $this->county = $addr->county;
        $this->detail = $addr->detail;
    }

    public function setPaytype($pay_type)
    {
        if(!in_array($pay_type,[self::PAY_ALIPAY,self::PAY_WX]));
            $pay_type = self::PAY_NOTKNOW;
        $this->pay_type = $pay_type;
    }
}
